<?php

/*
 * This file will return the document information which will be used for track document, which is above the log.
 * 
 * Created by: Peter Agno Jr.
 * Date created: September 29, 2011
 * 
 * In parameters: documentId, stepId
 * Out parameters: documentName, documentId, firstName, lastName, middleName, activity, departmentName, positionName
 */

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    // INCLUDES CONFIGURATION VARIABLES FOR DATABASE ACCESS
    include('../includes/siteConfig.php');
    
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // GET VARIABLES VIA POST METHOD
    $documentId = $_POST['documentId'];
    $stepId = $_POST['stepId'];

    // QUERY THE DOCUMENT NAME
    $query = 
        "
            SELECT documentName
            FROM MainDocument as MD, DocumentSetup as DS
            WHERE mainDocumentId = '$documentId' AND MD.documentSetupId = DS.documentSetupId
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    $documentName = $row['documentName'];
    
    // QUERY -GET SPECIFIC DOCUMENT INFORMATION FOR TRACKING
    $query = 
        "
            -- Get full name
            SELECT documentId, Account.userId as userId, firstName, lastName, middleName, activity
            FROM

            -- Get needed information in Log table
                (SELECT documentId, userId, activity 
                FROM Log 
                WHERE documentId = '$documentId' AND stepId = '$stepId'
                ORDER BY logDateTime ASC) as LogInfo

            LEFT JOIN Account
            ON LogInfo.userId = Account.userId
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);

    // DOCUMENT NAME
    $feed['documentName'] = $documentName;
    
    // DOCUMENT ID
    if( $row['documentId'] != NULL )
        $feed['documentId'] = $row['documentId'] . " ";
    else
        $feed['documentId'] = $documentId;
    
    // FULL NAME
    if( $row['firstName'] != NULL )
        $feed['firstName'] = $row['firstName'] . " ";
    else
        $feed['firstName'] = " ";
    
    if( $row['middleName'] != NULL )
        $feed['middleName'] = $row['middleName'] . " ";
    else
        $feed['middleName'] = " ";
    
    if( $row['lastName'] != NULL )
        $feed['lastName'] = $row['lastName'] . " ";
    else
        $feed['lastName'] = " ";
    
    /**************************************************************************/
    // DEPARTMENTS AND POSITIONS
    $userId = $row['userId'];
    if( $row['firstName'] == NULL && $row['middleName'] == NULL && $row['lastName'] == NULL ) {
        // IF THERE IS NO YET RECEIVER, THEN GET THE DEPARTMENTS AND POSITIONS OF THE POSSIBLE RECEIVERS
        $feed['firstName'] = "None";
        $feed['middleName'] = "";
        $feed['lastName'] = "";
        
        // GET DEPARTMENTS AND POSITIONS
        $query =
            "
                -- Get departmentName and positionName
                SELECT stepId, DP.deptPosId as deptPosId, DP.departmentId as departmentId, DP.positionId as positionId, departmentName, positionName
                FROM RouteStep_Dept_Pos as RDP, Department_Position as DP, Department as D, Position as P
                WHERE stepId = '$stepId' AND RDP.deptPosId = DP.deptPosId AND DP.departmentId = D.departmentId AND DP.positionId = P.positionId
                ORDER BY departmentName ASC, positionName ASC
            ";

        $deptPosResult = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        // SET OR CONNECT THE QUERY TO THE FEED
        if ( mysql_num_rows($deptPosResult) > 1 ) {
            // DISPLAY MULTIPLE DEPARTMENTS AND POSITIONS

            // DISPLAY DEPARTMENT                       
//            $feed['departmentName'] = "<br><ol type='A' style='padding-left: 25%;'>";
//            for( $index = 0 ; $index < mysql_num_rows($deptPosResult) ; $index++ ){
//                mysql_data_seek($deptPosResult, $index);
//                $deptPosRow = mysql_fetch_array($deptPosResult);
//                $feed['departmentName'] = $feed['departmentName']."<li>".$deptPosRow['departmentName']."</li>";
//            }
//            $feed['departmentName'] = $feed['departmentName']."</ol>";

            // DISPLAY POSITION            
//            $feed['positionName'] = "<br><ol type='A' style='padding-left: 25%;'>";
//            for( $index = 0 ; $index < mysql_num_rows($deptPosResult) ; $index++ ){
//                mysql_data_seek($deptPosResult, $index);
//                $deptPosRow = mysql_fetch_array($deptPosResult);
//                $feed['positionName'] = $feed['positionName']."<li>".$deptPosRow['positionName']."</li>";
//            }
//            $feed['positionName'] = $feed['positionName']."</ol>";
            
            // DISPLAY DEPARTMENTS AND POSITIONS IN LIST
            $firstRow = true;
            $feed['deptPos'] = "<ul style='padding-left: 15%;'>";
            while ( $deptPosRow = mysql_fetch_array($deptPosResult) ) {
                if ( $firstRow == true ) {
                    $firstRow = false;
                    $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>";
                }
                else
                    $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='border:0;'></td><td style='border:0;'>";
                
                $feed['deptPos'] = $feed['deptPos']."<li>".$deptPosRow['departmentName'] . " - " . $deptPosRow['positionName']."</li></td></tr>";
            }
            $feed['deptPos'] = $feed['deptPos']."</ul>";
        }
        else {
            // DISPLAY ONE DEPARTMENT AND POSITION
            $deptPosRow = mysql_fetch_array($deptPosResult);
            $feed['deptPos'] = "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>".$deptPosRow['departmentName']." - ".$deptPosRow['positionName']."</td></tr>";
//            $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>".$deptPosRow['positionName']."</td></tr>";
        }
    }
    else {
        // THERE IS A RECEIVER
        // GET DEPARTMENTS AND POSITIONS OF THAT RECEIVER
        $query =
            "
                -- Get departmentName and positionName
                SELECT userId, stepId, DP.deptPosId as deptPosId, DP.departmentId as departmentId, DP.positionId as positionId, departmentName, positionName
                FROM

                        -- Get the deptPosId of the user and also check if the user's deptPosId is connected with the step 
                        (SELECT userId, stepId, RDP.deptPosId as deptPosId
                        FROM
                                (SELECT *
                                FROM Account_Dept_Pos
                                WHERE userId = '$userId') as ADP,
                                (SELECT *
                                FROM RouteStep_Dept_Pos
                                WHERE stepId = '$stepId') as RDP

                        WHERE ADP.deptPosId = RDP.deptPosId) as UserStepInfo, Department_Position as DP, Department as D, Position as P

                WHERE UserStepInfo.deptPosId = DP.deptPosId AND DP.departmentId = D.departmentId AND DP.positionId = P.positionId
            ";
        
        $deptPosResult = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
                    
        // IF THERE WAS A CONNECTION BETWEEN THE USER AND STEP, IT MEANS THAT
        // THE POSITION OF THE USER, IS ONE OF THE SETUP POSITIONS FOR THAT STEP
        if ( mysql_num_rows($deptPosResult) != 0 ) {
            if ( ( mysql_num_rows($deptPosResult) - 1 ) > 0 ) {
                // DISPLAY ALL OF THE DEPARTMENTS AND POSITIONS OF USER IF IT HAS MORE THAN ONE
                
                // DISPLAY DEPARTMENTS AND POSITIONS IN LIST
                $firstRow = true;
                $feed['deptPos'] = "<ul style='padding-left: 15%;'>";
                while ( $deptPosRow = mysql_fetch_array($deptPosResult) ) {
                    if ( $firstRow == true ) {
                        $firstRow = false;
                        $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td>";
                    }
                    else
                        $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='border:0;'></td><td style='border:0;'>";

                    $feed['deptPos'] = $feed['deptPos']."<li>".$deptPosRow['departmentName'] . " - " . $deptPosRow['positionName']."</li></td></tr>";
                }
                $feed['deptPos'] = $feed['deptPos']."</ul>";
            }
            else {
                // DISPLAY ALL OF THE DEPARTMENT AND POSITION OF USER IF IT HAS ONLY ONE
                $deptPosRow = mysql_fetch_array($deptPosResult);
                $feed['deptPos'] = "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>".$deptPosRow['departmentName']." - ".$deptPosRow['positionName']."</td></tr>";
//                $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>".$deptPosRow['positionName']."</td></tr>";
            }
        }
        else {
            // ELSE, SEARCH FOR ALL OF THE USER'S POSITION
            $query =
                "
                    -- Get departmentName and positionName
                    SELECT userId, DP.deptPosId as deptPosId, DP.departmentId as departmentId, DP.positionId as positionId, departmentName, positionName
                    FROM Account_Dept_Pos as ADP, Department_Position as DP, Department as D, Position as P
                    WHERE userId = '$userId' AND ADP.deptPosId = DP.deptPosId AND DP.departmentId = D.departmentId AND DP.positionId = P.positionId
                ";

            $deptPosResult = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
            
            if ( ( mysql_num_rows($deptPosResult) - 1 ) > 0 ) {
                // DISPLAY ALL OF THE DEPARTMENTS AND POSITIONS OF USER IF IT HAS MORE THAN ONE
                
                // DISPLAY DEPARTMENTS AND POSITIONS IN LIST
                $firstRow = true;
                $feed['deptPos'] = "<ul style='padding-left: 15%;'>";
                while ( $deptPosRow = mysql_fetch_array($deptPosResult) ) {
                    if ( $firstRow == true ) {
                        $firstRow = false;
                        $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>";
                    }
                    else
                        $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='border:0;'></td><td style='border:0;'>";

                    $feed['deptPos'] = $feed['deptPos']."<li>".$deptPosRow['departmentName'] . " - " . $deptPosRow['positionName']."</li></td></tr>";
                }
                $feed['deptPos'] = $feed['deptPos']."</ul>";
            }
            else {
                // DISPLAY ALL OF THE DEPARTMENT AND POSITION OF USER IF IT HAS ONLY ONE
                $deptPosRow = mysql_fetch_array($deptPosResult);
                $feed['deptPos'] = "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>".$deptPosRow['departmentName']." - ".$deptPosRow['positionName']."</td></tr>";
//                $feed['deptPos'] = $feed['deptPos'] . "<tr><td style='padding-left:0px; border:0;' class='trackTableTd'>Position : </td><td style='border:0;'>".$deptPosRow['positionName']."</td></tr>";
            }
        }
    }
    mysql_free_result($deptPosResult);
    // DEPARTMENTS AND POSITIONS
    /**************************************************************************/

    // ACTIVITY
//    if( $row['activity'] != NULL )
//        $feed['activity'] = $row['activity'];
//    else {
        // QUERY THE POSSIBLE ACTIVITY
//        $query =
//            "
//                SELECT role
//                FROM RouteStep as RS
//                WHERE stepId = '$stepId'
//            ";
//        
//        $actResult = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
//        
//        $actRow = mysql_fetch_array($actResult);
//        
//        
//        if ( $actRow['role'] == 3 )
//            $feed['activity'] = "Archived";
//        else {
//            if ( $actRow['role'] == 2 )
//                $feed['activity'] = "Evaluate";
//            else 
//                $feed['activity'] = "Received";
//        }
//        
//        mysql_free_result($actResult);
//    }

    echo json_encode($feed);
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
